const cors = require('cors');//跨域
const bodyParser = require('body-parser');//解析参数
const express = require('express');
const app = express();
const mysql = require('mysql');
const router  =express.Router();
const option={
    //链接数据库的基本配置
    host:'localhost',
    user:'root',
    password:'root',
    port:'3306',
    database:'node_schema',
    connecTimeout:5000,//连接超时
    multipleStatements:false,//是否允许query中多条语句
};

// https://www.jianshu.com/p/

app.use(cors());//解决跨域问题
app.use(bodyParser.json());//json请求
app.use(bodyParser.urlencoded({extended:false}));

let pool;
repool();


function Result({code=1,msg='',data={}}){
    this.code = code;
    this.msg = msg;
    this.data = data;
}

function repool(){
    pool = mysql.createPool({
    ...option,
    waitForConnections:true,
    connectionLimit:100,
    queueLimit:0,
    });
    pool.on('error',err=>err.code === 'PROTOCOLCONNECTION LOST' && setTimeout(repool,2000))
}

module.exports={pool,Result,app,router};
